/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Handles Saving and loading configuration
 * 
 * --CHANGELOG--
 * 
 * */

using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace DOMercury.Managers
{
    class ConfigurationManager
    {
        public static bool ShowSplashScreen = true;
        public static bool WindowsKey = true;
        public static bool AltKey = false;
        public static bool ShiftKey = false;
        public static bool CtrlKey = false;
        public static string KeyCode = "Z";
        public static string CurrentInterface = "SkinnableForm(Original DOMercury Interface)";
        public static bool ShowFailAlerts = true;
        public static bool SmartSearch = true;
        public static int MaxResults = 20;
        public static bool CheckUpdates = true;
        public static DateTime LastChecked = DateTime.Now.Subtract(TimeSpan.FromDays(1));

        public static void LoadCurrentConfiguration()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("config.xml");
            try
            {
                if (doc.DocumentElement.SelectSingleNode("WindowsKey") != null)
                {
                    WindowsKey = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("WindowsKey").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("AltKey") != null)
                {
                    AltKey = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("AltKey").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("ShiftKey") != null)
                {
                    ShiftKey = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("ShiftKey").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("CtrlKey") != null)
                {
                    CtrlKey = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("CtrlKey").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("ShowFailAlerts") != null)
                {
                    ShowFailAlerts = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("ShowFailAlerts").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("SmartSearch") != null)
                {
                    SmartSearch = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("SmartSearch").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("KeyCode") != null)
                {
                    KeyCode = doc.DocumentElement.SelectSingleNode("KeyCode").InnerText;
                }
                if (doc.DocumentElement.SelectSingleNode("MaxResults") != null)
                {
                    MaxResults = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("MaxResults").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("LastChecked") != null)
                {
                    LastChecked = DateTime.Parse(doc.DocumentElement.SelectSingleNode("LastChecked").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("CheckUpdates") != null)
                {
                    CheckUpdates = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("CheckUpdates").InnerText);
                }
                if (doc.DocumentElement.SelectSingleNode("CurrentInterface") != null)
                {
                    CurrentInterface = doc.DocumentElement.SelectSingleNode("CurrentInterface").InnerText;
                }
                if (doc.DocumentElement.SelectSingleNode("ShowSplashScreen") != null)
                {
                    ShowSplashScreen = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("ShowSplashScreen").InnerText);
                }
            }
            catch
            {
                //System.Windows.Forms.MessageBox.Show("Config.xml is malformed, a required element is missing.  Please fix or reinstall.");
            }
        }

        public static void SaveCurrentConfiguration()
        {
            XmlTextWriter w = new XmlTextWriter("config.xml", Encoding.ASCII);
            w.WriteStartElement("configuration");
            w.WriteStartElement("WindowsKey");
            w.WriteRaw(WindowsKey.ToString());
            w.WriteEndElement();
            w.WriteStartElement("AltKey");
            w.WriteRaw(AltKey.ToString());
            w.WriteEndElement();
            w.WriteStartElement("ShiftKey");
            w.WriteRaw(ShiftKey.ToString());
            w.WriteEndElement();
            w.WriteStartElement("CtrlKey");
            w.WriteRaw(CtrlKey.ToString());
            w.WriteEndElement();
            w.WriteStartElement("KeyCode");
            w.WriteRaw(KeyCode.Trim().Replace("\"", ""));
            w.WriteEndElement();
           w.WriteStartElement("ShowFailAlerts");
            w.WriteRaw(ShowFailAlerts.ToString());
            w.WriteEndElement();
           w.WriteStartElement("SmartSearch");
            w.WriteRaw(SmartSearch.ToString());
            w.WriteEndElement();
            w.WriteStartElement("MaxResults");
            w.WriteRaw(MaxResults.ToString());
            w.WriteEndElement();
           w.WriteStartElement("LastChecked");
            w.WriteRaw(LastChecked.ToString());
            w.WriteEndElement();
            w.WriteStartElement("CheckUpdates");
            w.WriteRaw(CheckUpdates.ToString());
            w.WriteEndElement();
            w.WriteStartElement("CurrentInterface");
            w.WriteRaw(CurrentInterface.ToString());
            w.WriteEndElement();
            w.WriteStartElement("ShowSplashScreen");
            w.WriteRaw(ShowSplashScreen.ToString());
            w.WriteEndElement();
            w.WriteEndElement();

            try
            {
                w.Close();
            }
            catch { }
        }
    }
}
